Skip to main content

Workflow Schedule Template

The Workflow Schedule template demonstrates how BindAI represents and tracks workflows that are intended to execute at specific times or recurring intervals. A WorkflowSchedule stores the workflow identifier, the next scheduled execution time, an optional recurring interval, and whether the schedule is enabled. The WorkflowScheduler maintains registered schedules and identifies which schedules are currently due.

Purpose

This template demonstrates how to:
  • create a workflow schedule
  • register schedules with a scheduler
  • determine which schedules are due
  • configure recurring execution intervals
  • enable or disable schedules
  • advance recurring schedules after execution
Scheduling is kept separate from workflow execution itself.

Scheduling Architecture

The scheduling components are responsible for determining when a workflow should run.
The scheduler does not execute the workflow itself. It identifies schedules that are ready for execution so that application code can start the corresponding workflow.

Workflow Schedule

A WorkflowSchedule describes one scheduled workflow. The current implementation contains four fields:
The fields are:

Registering a Schedule

Schedules are registered with WorkflowScheduler.
The scheduler stores the schedule in its internal schedule collection. Multiple schedules can be registered with the same scheduler.

Checking Due Schedules

The scheduler provides the due() method to identify schedules whose execution time has arrived. Conceptually:
Only schedules that are:
  • enabled, and
  • whose next_run is at or before the current time
are returned.

Due Schedule Example

Suppose the scheduler contains three schedules:
If the current time is 10:30:
Calling:
returns the schedules that are currently due.

Enabled Schedules

Only enabled schedules are considered by due().
An enabled schedule can be returned when its next_run has arrived.

Disabled Schedules

A disabled schedule remains registered but is ignored when checking for due schedules.
Even if its next_run is in the past, due() does not return it. This allows applications to temporarily disable scheduled workflows without removing their schedule.

One-Time Schedule

A schedule without an interval can represent a one-time execution.
After the schedule becomes due, calling reschedule() does not move its next_run because no recurring interval is configured. The application can therefore treat the schedule as a one-time schedule.

Recurring Schedule

A recurring schedule specifies interval_seconds.
After the schedule has been processed, the scheduler can advance it:
The next execution time is calculated by adding the configured interval.

Rescheduling

The reschedule() method updates recurring schedules. Conceptually:
For example:
After rescheduling:
If interval_seconds is not configured, the schedule is not advanced.

Scheduler Lifecycle

A typical application-level scheduling cycle looks like this:
The current WorkflowScheduler provides the registration, due-checking, and rescheduling portions of this lifecycle. The surrounding application is responsible for deciding how and when to call these methods and how to execute the returned workflows.

Scheduler Does Not Execute Workflows

The scheduler itself does not invoke a workflow executor. Its responsibility is to identify schedules that are ready.
This separation keeps scheduling logic independent from workflow execution.

Scheduler vs Workflow Executor

The scheduler and executor have different responsibilities. The scheduler answers when should this workflow run? The executor answers how should this workflow execute?

Scheduler vs Loop

Scheduling and loops both support repeated behavior, but at different levels. A scheduler controls when a workflow starts. A loop controls how a running workflow repeats.

Example: Hourly Workflow

An application may register an hourly workflow:
The application periodically checks:
The scheduler therefore maintains the timing state while workflow execution remains separate.

Multiple Schedules

A single scheduler can contain multiple workflows.
Calling due() evaluates all registered schedules and returns the enabled schedules whose next_run has arrived.

Current Time Handling

The current implementation determines due schedules using:
Therefore, the scheduler currently compares next_run against the UTC-based current time returned by datetime.utcnow(). Applications should ensure that the timestamps supplied to WorkflowSchedule are consistent with the time basis expected by the scheduler. Timezone conversion and user-facing local scheduling are application-level concerns and are not currently implemented by WorkflowScheduler.

Recurring Execution

Recurring schedules are represented using an interval rather than a separate recurrence rule. For example:
represents a 24-hour interval. Common examples include:
The scheduler simply adds the configured number of seconds to next_run.

Typical Use Cases

The scheduling mechanism can support workflows such as:
  • periodic reports
  • recurring data synchronization
  • scheduled document processing
  • periodic knowledge refreshes
  • recurring maintenance
  • automated backups
  • monitoring workflows
  • periodic AI processing
The actual workflow execution remains handled by the workflow system outside the scheduler.

Schedule State

A schedule has a small and explicit state model:
This makes schedules simple to persist, inspect, and manage.

Best Practices

  • Use a consistent time basis for next_run.
  • Keep recurring intervals explicit.
  • Disable schedules that should temporarily stop running.
  • Reschedule recurring schedules after processing them.
  • Keep scheduling separate from workflow execution.
  • Avoid registering duplicate schedules unintentionally.
  • Monitor overdue schedules in the surrounding application.
  • Ensure scheduled workflows can safely execute more than once when using recurring schedules.

Related Templates

The Schedule template works well alongside:
  • Workflow Retry
  • Workflow Timeout
  • Workflow Parallel
  • Workflow Human
Scheduling determines when execution should occur, while these other workflow features determine how execution behaves.

Summary

The Workflow Schedule template demonstrates how BindAI represents and manages time-based workflow execution. WorkflowSchedule stores the workflow identifier, next execution time, optional recurrence interval, and enabled state. WorkflowScheduler provides three core operations:
Together, these components allow applications to register scheduled workflows, identify which schedules are due, and advance recurring schedules while keeping scheduling separate from actual workflow execution.